/*
 * 该文件由 @ali/aisc-i18n 生成，如无特殊情况，请勿更改
 *
 * 参考文档:
 * @ali/intl-universal 集团 i18n 基础库 : http://gitlab.alibaba-inc.com/parrot/intl-universal
 * Melody - 文案测试和审查插件: https://yuque.antfin-inc.com/kaihe.hkh/melody/tutorial
 */
const { getLocale } = require('@alife/yunxiao-common');
/* eslint-disable strict */
const { provideIntl, destroyIntl } = require('@ali/intl-universal');
// 美杜莎应用名
const medusaName = 'code-assets';

// 下面的链接定义了window['code-assets_develop']
// http://lang.alicdn.com/daily/code-assets/0.0.1/code-assets_develop.json
// window['code-assets_develop'] = {
//   "zh_CN":{"code-assets.mdEditor.n3-search.n3-search.PressEscToCloseThe":"按ESC键关闭面板","code-assets.src.utils.locale.PublicKey":"公钥","code-assets.src.utils.locale.ReviewDescription":"评审描述","code-assets.components.basicInfo.AssociatedChanges":"关联变更","code-assets.pricing.containers.dataSource.CustomerCase":"客户案例","code-assets.projectMergeRequestDetail.containers.root.SubmissionHistory":"提交历史"}
// }
const locale = window['code-assets_develop'] || {
  zh_CN: {},
  en_US: {},
};

// const locale = require('./strings/index');

let intl; // Instance of intl-universal. Create by provideIntl

const internetDomain = 'aliyun.com';
const inMelodyEnv =
  internetDomain.includes('127.0.0.1') ||
  internetDomain.includes('localhost') ||
  internetDomain.includes('aliyun.test'); // 仅在日常和本地环境支持 melody 插件输出 key 语言
const forceMelody =
  medusaName && inMelodyEnv && window.location.search.includes('enableMelody=true');

/**
 * update instance of intl universal
 */
function update() {
  destroyIntl();
  const language = getLocale() || (window.FORCE && window.FORCE.locale) || 'zh_CN';
  const messagesAIO = {
    [language]: locale[language],
  };

  intl = provideIntl({
    locale: language,
    messagesAIO,
  });
}

/**
 * Format string by key
 * For example:
 * $i18n.get('jsx.home.title'),
 * $i18n.get({
 *    id: 'jsx.home.hello',
 *    defaultMessage: 'Hello {name}' // not required
 * },{
 *  name: 'Alice'
 * })
 * More syntax: https://formatjs.io/guides/message-syntax/
 * @param {string|object} id key or object
 * @param {object} variable variable for id
 * @return {string} format message
 */
function get(id, variable) {
  if (!window['code-assets_develop']) {
    variable &&
      Object.keys(variable).forEach((v) => {
        const reg = new RegExp(`{${v}}`, 'g');
        id.defaultMessage = id.defaultMessage.replace(reg, variable[v]);
      });
    return id.defaultMessage;
  }
  if (forceMelody) {
    return getWithMelody(id, variable);
  }
  if (!intl) update();
  if (typeof id === 'string') {
    return intl.formatMessage({ id }, variable);
  } else if (typeof id === 'object' && id.dm) {
    id.defaultMessage = id.dm;
  }
  return intl.formatMessage(id, variable);
}

function getWithMelody(id, variable) {
  if (!intl) update();
  if (typeof id === 'string') {
    let text = intl.formatMessage({ id }, variable);
    text = `##@@@${id}##${medusaName}@@@##${text}`;
    return text;
  } else if (typeof id === 'object' && id.dm) {
    id.defaultMessage = id.dm;
  }
  let text = intl.formatMessage(id, variable);
  text = `##@@@${id.id}##${medusaName}@@@##${text}`;
  return text;
}

// * Configuration document: http://gitlab.alibaba-inc.com/parrot/intl-common-info
update();

module.exports = {
  get,
  formatMessage: get,
};
